import unittest
import process_ebk as ebk
import file_operations as file
from document import *

class TestProcessEbk(unittest.TestCase):
    def setUp(self):
        FILE_ADDRESS = "data/ebooks/35.ebk"
        self.ebk_content = file.read(FILE_ADDRESS)
        self.new_doc = document([], "some ebook", "ebk")
        self.header, self.body = ebk.getHeaderAndBody(self.ebk_content)
        self.header_list = ebk.makeHeaderList(self.header)
        
    def testGetHeaderAndBody(self):
        self.assertEqual(self.header, "Project Gutenberg's The Time Machine, by H. G. (Herbert George) Wells\n\nThis eBook is for the use of anyone anywhere at no cost and with\nalmost no restrictions whatsoever.  You may copy it, give it away or\nre-use it under the terms of the Project Gutenberg License included\nwith this eBook or online at www.gutenberg.net\n\n\nTitle: The Time Machine\n\nAuthor: H. G. (Herbert George) Wells\n\nRelease Date: October 2, 2004 [EBook #35]\n\nLanguage: English\n\nCharacter set encoding: ASCII\n\n")
        self.assertEqual(self.body, "\n\n\nThe Time Machine, by H. G. Wells [1898]\n\n\nI\n\nThe Time Traveller (for so it will be convenient to speak of him)\nwas expounding a recondite matter to us. His grey eyes shone and\ntwinkled, and his usually pale face was flushed and animated. The\nfire burned brightly, and the soft radiance of the incandescent\nlights in the lilies of silver caught the bubbles that flashed and\nthinking of beginning the")
    
    def testParseEbk(self):
        x = ebk.parseEbk(self.ebk_content, self.new_doc)
        self.assertEqual(x, ['machin', 'h', 'g', 'well', '1898', 'travel', 'conveni', 'speak', 'expound', 'recondit', 'matter', 'grei', 'shone', 'twinkl', 'pale', 'flush', 'anim', 'fire', 'burn', 'brightli', 'soft', 'radianc', 'incandesc', 'light', 'lili', 'silver', 'caught', 'bubbl', 'flash', 'think', 'begin'])
        self.assertEqual(self.header, "Project Gutenberg's The Time Machine, by H. G. (Herbert George) Wells\n\nThis eBook is for the use of anyone anywhere at no cost and with\nalmost no restrictions whatsoever.  You may copy it, give it away or\nre-use it under the terms of the Project Gutenberg License included\nwith this eBook or online at www.gutenberg.net\n\n\nTitle: The Time Machine\n\nAuthor: H. G. (Herbert George) Wells\n\nRelease Date: October 2, 2004 [EBook #35]\n\nLanguage: English\n\nCharacter set encoding: ASCII\n\n")
    
    def testParseEbkHead(self):
        ebk.parseEbk(self.ebk_content, self.new_doc)
        self.assertEqual(self.new_doc.title, ['machine'])
        self.assertEqual(self.new_doc.input_title, 'The Time Machine ')  
        self.assertEqual(self.new_doc.authors, ['h', 'g', 'herbert', 'george', 'wells'])
        self.assertEqual(self.new_doc.input_authors, 'H. G. (Herbert George) Wells ')

    def testProcessTitle(self):
        ebk.processTitle(self.header_list, self.new_doc)
        self.assertEqual(self.new_doc.title, ['machine'])  
        self.assertEqual(self.new_doc.input_title, 'The Time Machine ')  
    
    def testProcessAuthor(self):
        ebk.processAuthor(self.header_list, self.new_doc)
        self.assertEqual(self.new_doc.authors, ['h', 'g', 'herbert', 'george', 'wells'])
        self.assertEqual(self.new_doc.input_authors, 'H. G. (Herbert George) Wells ')
    
    def testMakeHeaderList(self):
        self.assertEqual(self.header_list, ['Project', "Gutenberg's", 'The', 'Time', 'Machine,', 'by', 'H.', 'G.', '(Herbert', 'George)', 'Wells', 'This', 'eBook', 'is', 'for', 'the', 'use', 'of', 'anyone', 'anywhere', 'at', 'no', 'cost', 'and', 'with', 'almost', 'no', 'restrictions', 'whatsoever.', 'You', 'may', 'copy', 'it,', 'give', 'it', 'away', 'or', 're-use', 'it', 'under', 'the', 'terms', 'of', 'the', 'Project', 'Gutenberg', 'License', 'included', 'with', 'this', 'eBook', 'or', 'online', 'at', 'www.gutenberg.net', 'Title:', 'The', 'Time', 'Machine', 'Author:', 'H.', 'G.', '(Herbert', 'George)', 'Wells', 'Release', 'Date:', 'October', '2,', '2004', '[EBook', '#35]', 'Language:', 'English', 'Character', 'set', 'encoding:', 'ASCII'])
    
    
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(TestProcessEbk)
    unittest.TextTestRunner(verbosity=2).run(suite)